#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import re
from typing import List, Dict
from .base import FilterRule

class MarkdownHeadingLevelFilter(FilterRule):
    """Markdown标题级别变化过滤规则"""
    
    def should_filter(self, add_content: str, remove_content: str, hunk_changes: List[Dict]) -> bool:
        """判断是否为仅改变Markdown标题级别的修改"""
        add_clean = add_content.strip()
        remove_clean = remove_content.strip()
        
        # 允许有无空格的 Markdown 标题
        heading_pattern = r'^#{1,6}\s*'
        
        add_is_heading = re.match(heading_pattern, add_clean)
        remove_is_heading = re.match(heading_pattern, remove_clean)
        
        if not (add_is_heading and remove_is_heading):
            return False
        
        add_text = re.sub(heading_pattern, '', add_clean)
        remove_text = re.sub(heading_pattern, '', remove_clean)
        
        add_level = len(add_clean) - len(add_clean.lstrip('#'))
        remove_level = len(remove_clean) - len(remove_clean.lstrip('#'))
        
        return add_text.strip() == remove_text.strip() and add_level != remove_level
    
    def get_filter_reason(self) -> str:
        return "markdown_heading_level_change" 